//获得节点
function getIds(obj) {
	return document.getElementById(obj);
}

//获得name
function getNames(nam){
	return document.getElementsByName(nam);
}

//获得标签
function getTagNam(tname){
	return document.getElementsByTagName(tname);
}

//创建行节点
function createEle(jie){
	var a = document.createElement(jie);
	var b = getIds("address").parentNode.parentNode
	var c = b.parentNode
	return c.insertBefore(a,b);
}
function createEletd(father,jie){
	var a = document.createElement(jie);
	return father.appendChild(a);
}

//获得地址
function getS(keys) {
	return sessionStorage.getItem(keys);
}
//显示地址
function address() {	
	var diva = getIds("diva")
	diva.style.display="block"
	
	/*window.showModalDialog("address.html");*/
	/*getIds("address").value = getS("sheng") + getS("city") + getS("qu") + getS("jie") + getS("men")*/
}

//数量
function shuliang(val) { 
	//获取一行内的各个值
	var j;
	var s =0;
	var h1 = val.parentNode.nextElementSibling.firstChild
	var h2 = val.parentNode.nextElementSibling.nextElementSibling.firstChild
	var h3 = val.parentNode.nextElementSibling.nextElementSibling.nextElementSibling.firstChild
	
	var dan = parseFloat(h1.value);
	var yun = parseFloat(h2.value);
	var zong = parseFloat(h3.value);
		// 加减操作
	switch(val.value) {
		case "+":
			j = val.parentNode.firstChild.nextElementSibling//获取到数量的值
			s = parseInt(j.value);
			j.value = s + 1;
			s = parseInt(j.value);
			h3.value = (s * dan + yun) + "元"
			break;
		case "-":
			j = val.nextElementSibling;//获取到数量的值
			s = parseInt(j.value);
			if(s > 1) {
				s = parseInt(j.value);
				j.value = s - 1;
				s = parseInt(j.value);
				h3.value = (s * dan + yun) + "元"
			} else if(s <= 1) {
				alert("几个意思？？？");//数量不可小于1
			}
			break;
		default:
			break;
	}
	zongji();

}

//提交订单
function tidi() {
	var address = getIds("address").value
	var name = document.getElementsByName("sname")
	var shu = document.getElementsByName("shu")
	var dan = document.getElementsByName("dan")
	var yun = document.getElementsByName("yun")
	var zong = document.getElementsByName("zong")
	var szong = getIds("zongji");
	var sname = sshu = sdan = syun = "";
	
	if(address==""){
		alert("请填写收货地址!!!")
		return false
	}
	if(szong.value==0){
		alert("请先添加商品!!!")
		return false
	}
	
	for(var i=0;i<name.length;i++){
		sname = sname + name[i].value+"   ["+ shu[i].value+"]个       "+"单价:"+dan[i].value+"元       "+"运费:"+ yun[i].value+"元"+"\n" ;
	}
	var b = confirm("您本次购买的商品信息如下:"+ "\n"+sname+ "费用合计:" +szong.value+ "元\n" + "送货地址:" +address+ "\n" + "----请确认以上信息是否有误!!!----");
	if(b) {
		alert("提交成功！")
		return true
	} else {
		alert("已取消！！")
		return false
	}
}

//选择
function quanbu(){
	var c = getNames("checkB");
	var q = getIds("all");
	//无商品不可全选
	if(c.length==0){
		q.checked = false;
	}
	//全选全被选中
	for(var i=0;i<c.length;i++){
		c[i].checked = q.checked;
	}
}

//反选
function fanxuan(){
	var c = getNames("checkB");
	var q = getIds("all");
	var count=0;
	//计算为true的个数
	for(var i=0;i<c.length;i++){
		if(c[i].checked==true){
			count++;
		}
	}
	//如果全被选中则全选为TRUE
	if(count==c.length){
		q.checked=true
	}else{
		q.checked=false
	}
}

//添加商品
function add(){
	//获取值
	var tab = getIds("tab")
	var name = getIds("name").value;
	var price = getIds("price").value;
	var count = getIds("count").value;
	var yf = getIds("yf").value;
	var zong = parseFloat(price)*parseFloat(count)+parseFloat(yf)
	
	if(name==""||price==""||count==""||yf==""){
		alert("请填写完整！！！");
		return;
	}
	
	//创建节点
	var t = createEle("tr")
	var td1 = createEletd(t,"td")
	var td2 = createEletd(t,"td")
	var td3 = createEletd(t,"td")
	var td4 = createEletd(t,"td")
	var td5 = createEletd(t,"td")
	var td6 = createEletd(t,"td")
	var td7 = createEletd(t,"td")
	//写入
	td1.innerHTML="<input type='checkbox' name='checkB'  onclick='fanxuan()' />"
	td2.innerHTML="<input class='kuang' type='text' value='"+name+"' readonly='readonly' name='sname' id='sname'>";
	td3.innerHTML="<input class='anniu' type='button' value='-' id='jian' onclick='shuliang(this)'><input type='text' value='"+count+"' name='shu' readonly='readonly' id='shu'><input class='anniu' type='button' value='+' id='jia' onclick='shuliang(this)'>";
	td4.innerHTML="<input class='kuang' type='text' value='"+price+"' readonly='readonly' name='dan' id='dan'>";
	td5.innerHTML="<input class='kuang' type='text' value='"+yf+"' readonly='readonly' name='yun' id='yun'>";
	td6.innerHTML="<input class='kuang' type='text' value='"+zong+"' readonly='readonly' name='zong' id='zong'>"
	td7.innerHTML="<a href='#' onclick='del(this)'>删除</a>"
	
	//清空输入框
	getIds("name").value = ""
	getIds("price").value = ""
	getIds("count").value = ""
	getIds("yf").value = ""
	
	zongji();
}

//总计
function zongji(){
	var zongji = getIds("zongji")
	var zong = document.getElementsByName("zong")
	zongji.value = 0
	for(var i=0;i<zong.length;i++){
		zongji.value = parseFloat(zongji.value) + parseFloat(zong[i].value) 
	}
	
}

//删除

function del(obj){
	var t = obj.parentNode.parentNode
	var ta = t.parentNode
	ta.removeChild(t);
	//如果全被删除则全选为false
	var c = getNames("checkB");
	var q = getIds("all");
	if(c.length==0){
		q.checked = false;
	}
	zongji()
}

//清空
function clears(){
	var c = getNames("checkB");
	//循环删除
	for(var i=0;i<c.length;){
		if(c[i].checked){
			del(c[i]);
			i=0;
			
		}else{
			i++;
		}
	}
zongji();
	
}
